package app.goods.info.views;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;

import org.apache.commons.lang3.StringUtils;

import app.goods.info.constants.ConstontValue;
import app.goods.info.constants.GlobalCache;
import app.goods.info.services.domain.GoodsInfo;
import app.goods.info.services.domain.ShopInfo;
import app.goods.info.utils.InterfaceUtils;

public class ColroTableCellRenderer extends DefaultTableCellRenderer {
	private JFrame frame;

	public ColroTableCellRenderer(JFrame frame) {
		super();
		this.frame = frame;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
			int row, int column) {
		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		if (column == 0) {
			Set<String> lostGoodsShops = new HashSet<String>();
			GlobalCache.getInstance().getTargetCataLogues().forEach(catalogue -> {
				// 当前品类的商品
				List<GoodsInfo> goodsInfos = GlobalCache.getInstance().getCatalogueGoodsInfos()
						.get(catalogue.getCatalogueName());
				// 某个品类中不存在的供应商
				Set<String> shopNames = goodsInfos.stream().map(GoodsInfo::getShopName).collect(Collectors.toSet());
				List<ShopInfo> targetShops = GlobalCache.getInstance().getAllShopInfos().stream()
						.filter(shopInfo -> shopInfo.getStatus().equals("ON")).collect(Collectors.toList());
				targetShops.forEach(shopInfo -> {
					if (!shopNames.contains(shopInfo.getShopName())) {
						lostGoodsShops.add(shopInfo.getShopName());
					}
				});
				// 某个品类下缺少商品的供应商
				// 当前品类下，Map<供应商，供应商包含的商品名称列表>
				Map<String, List<String>> shopGoodsNames = goodsInfos.stream().collect(Collectors.groupingBy(
						GoodsInfo::getShopName, Collectors.mapping(GoodsInfo::getGoodsName, Collectors.toList())));

				// 遍历当前分类下供应商列表
				shopGoodsNames.entrySet().forEach(entry -> {
					// 当前分类下每个供应商包含的所有的商品名称列表
					JTable namesTable = InterfaceUtils.getComponentByName(frame,
							"table-name-list-".concat(catalogue.getCatalogueName()), JTable.class);
					TableModel namesTableModel = namesTable.getModel();
					// 获取数量不为0的商品
					List<String> hasQuantityGoodsNames = new ArrayList<String>();
					int rowCount = namesTableModel.getRowCount();
					for (int i = 0; i < rowCount; i++) {
						String goodsName = (String) namesTableModel.getValueAt(i, 1);
						String goodsQuantity = (String) namesTableModel.getValueAt(i, 2);
						if (StringUtils.isNotEmpty(goodsQuantity) && !"0".equals(goodsQuantity)) {
							hasQuantityGoodsNames.add(goodsName);
						}
					}
					// 商品数量不为0的列表，删除次品类下所有的商品列表
					hasQuantityGoodsNames.removeAll(entry.getValue());
					// 如果hasQuantityGoodsNames数组内还有元素，说明这个供应商没有这个商品，设置背景色
					if (hasQuantityGoodsNames.size() > 0) {
						lostGoodsShops.add(entry.getKey());
					}
				});
			});
			if (lostGoodsShops.contains(value)) { // if a row is selected
				setBackground(Color.RED);
			} else {
				setBackground(Color.WHITE);
			}
			if (ConstontValue.SELF_SHOP.equals(value)) { // if a row is selected
				setBackground(Color.GREEN);
			}
		}
		return this;
	}
}
